<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>收费标准管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="咨询师名称">
					<el-select v-model="value" filterable remote reserve-keyword clearable @clear="queryList"
						placeholder="请输入咨询师名称" :remote-method="remoteMethod" :loading="loading">
						<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="收费类型">
					<el-select v-model="queryInfo.type_id" clearable @clear="queryList" placeholder="请输入收费类型">
						<el-option v-for="item in typeData" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddCharge">新增收费标准</el-button>
					<el-button type="danger" @click="deleteBatch">批量删除</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 新增和修改的对话框 -->
		<!-- dialog中绑定close事件，实现对话框右上角叉号的取消功能 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="charge">
				<el-form-item label="咨询师名称" label-width="100px">
					<el-select v-model="addConsult_id" filterable remote reserve-keyword clearable
						placeholder="请输入咨询师名称" :remote-method="remoteMethod" :loading="loading">
						<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="收费类型" label-width="100px">
					<el-select v-model="charge.type_id" clearable placeholder="请输入收费类型">
						<el-option v-for="item in typeData" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="收费价格" label-width="100px">
					<el-input placeholder="请输入收费价格" v-model="charge.chargePrice"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addCharge">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 表格区域 -->
		<el-table ref="multipleTable" @selection-change="handleSelectionChange" :data="tableData" border
			style="width: 100%;" :header-cell-style="{'text-align': 'center'}" :cell-style="{'text-align': 'center'}">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID" width="100"></el-table-column>
			<el-table-column prop="consult.name" label="咨询师名称">
			</el-table-column>
			<el-table-column prop="chargetype.name" label="收费类型">
			</el-table-column>
			<el-table-column prop="chargePrice" label="收费价格">
			</el-table-column>
			<el-table-column prop="countNumber" label="咨询人数">
			</el-table-column>
			<el-table-column label="操作" width="100">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditCharge(scope.row)">编辑</el-link><br />
					<el-link type="primary" @click="deleteCharge(scope.row.id)">删除</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				// 查询信息对象
				queryInfo: {
					"type_id": "",
					"consult_id": "",
					"pageNum": 1,
					"pageSize": 5
				},
				total: 10,
				// 表格数据
				tableData: [],
				// 类型
				typeData: [],
				// 咨询师
				loading: false,
				// 全部咨询师
				consultData: [],
				// 筛选咨询师
				consultSearchList: [],
				options: [], // 监听筛选
				// 最终咨询师id
				value: [],
				// 批量删除
				ids: null,
				// 对话框标题
				title: '',
				// 是否显示对话框
				dialogFormVisible: false,
				// 咨询师对象 charge
				charge: {
					"id": "",
					"type_id": "",
					"chargetype": {},
					"consult_id": "",
					"consult": {},
					"chargePrice": 0,
					"countNumber": 0,
				},
				addConsult_id: [],
			}
		},
		methods: {
			// 查询数据绑定查询对象
			setqueryInfo(t_id, c_id) {
				this.queryInfo.type_id = t_id;
				this.queryInfo.consult_id = c_id;
			},
			// 分页查询信息的方法
			queryList() {
				// console.log(this.value)
				this.setqueryInfo(this.queryInfo.type_id, this.value)
				// console.log(this.queryInfo)
				// 调用后端接口来获取数据
				this.$http.get('/charge/find', {
						params: this.queryInfo
					})
					.then(resp => {
						if (resp.data.status == 200) {
							this.tableData = resp.data.data.list;
							this.total = resp.data.data.total;
							// console.log(this.tableData)
						} else {
							this.$message.error('查询失败！')
						}
					})
					.catch((error) => {
						this.$message.error('服务器失联' + error)
					})
			},
			// 进入添加的对话框
			toAddCharge() {
				this.title = "新增收费标准";
				this.dialogFormVisible = true;
			},
			// 进入修改的预览数据页面
			toEditCharge(row) {
				// console.log(row)
				// 赋值
				this.charge = row;
				// 设置标题
				this.title = "修改收费标准";
				// 开启对话框
				this.dialogFormVisible = true;
			},
			// 添加数据绑定添加对象
			setaddInfo(t_id, c_id) {
				this.charge.type_id = t_id;
				this.charge.consult_id = c_id;
			},
			// 添加
			addCharge() {
				this.setaddInfo(this.charge.type_id, this.addConsult_id);
				if (this.charge.id) { // 有值为true  修改
					this.$http.put("/charge/edit", this.charge)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('修改成功！');
								// 清空数据
								this.charge = { // 对象
									"id": "",
									"type_id": "",
									"chargetype": {},
									"consult_id": "",
									"consult": {},
									"chargePrice": 0,
									"countNumber": 0,
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				} else {
					this.$http.post("/charge/add", this.charge)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('添加成功！');
								// 清空数据
								this.charge = { // 对象
									"id": "",
									"type_id": "",
									"chargetype": {},
									"consult_id": "",
									"consult": {},
									"chargePrice": 0,
									"countNumber": 0,
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				}
			},
			// 取消对话框
			cancle() {
				this.dialogFormVisible = false;
				this.charge = { // 对象
					"id": "",
					"type_id": "",
					"chargetype": {},
					"consult_id": "",
					"consult": {},
					"chargePrice": 0,
					"countNumber": 0,
				}
			},
			// 删除
			deleteCharge(id) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('/charge/remove/' + id)
							.then(resp => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 批量删除
			handleSelectionChange(val) {
				this.ids = val.map(function(item) {
					return item.id;
				});
				// console.log(this.ids);
			},
			// 批量删除
			deleteBatch() {
				if (this.ids.length == 0) { // 没有选择列表
					return;
				}
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('/charge/removeBatch?ids=' + this.ids)
							.then(resp => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 查询收费类型
			getChargetype() {
				// 调用后端接口来获取数据
				this.$http.get('/chargetype/find')
					.then(resp => {
						if (resp.data.status == 200) {
							this.typeData = resp.data.data.list;
						} else {
							this.$message.error('查询失败！')
						}
					})
					.catch((error) => {
						this.$message.error('服务器失联' + error)
					})
			},
			// 查询咨询师
			getConsult() {
				// 调用后端接口来获取数据
				this.$http.get('/consult/find')
					.then(resp => {
						if (resp.data.status == 200) {
							this.consultData = resp.data.data.list;
							this.consultSearchList = this.consultData.map(item => {
								return {
									value: `${item.id}`,
									label: `${item.name}`
								};
							});
						} else {
							this.$message.error('查询失败！')
						}
					})
					.catch((error) => {
						this.$message.error('服务器失联' + error)
					})
			},
			// 分页
			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.queryList();
			},

			// 监听效果
			remoteMethod(query) {
				// console.log(query)
				if (query !== '') {
					this.loading = true;
					setTimeout(() => {
						this.loading = false;
						this.options = this.consultSearchList.filter(item => {
							return item.label.toLowerCase()
								.indexOf(query.toLowerCase()) > -1;
						});
					}, 200);
				} else {
					this.options = [];
				}
			},


		},
		mounted() {
			this.queryList();
			this.getChargetype();
			this.getConsult();
			// 动态的获取tool.js中的url，并赋值给当前组件的全局url
			// this.url = url;
			// 原型链引入
			this.url = this.$url;
		},
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}

	/**
	 * 设置富文本编辑器的最小高度，注意style中不能使用scoped 
	**/
	.ql-container {
		min-height: 200px;
	}
</style>